﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using HotelMgr.Models;

namespace HotelMgr.Areas.Admin.Controllers
{
    [Authorize(Roles = "Administrators")]
    public class RoomManagerController : Controller
    {
        private HotelEntities db = new HotelEntities();

        //
        // GET: /RoomManager/ 

        public ViewResult Index()
        {
            var rooms = db.Rooms.Include(r => r.RoomType).Include(r => r.RoomState);
            return View(rooms.ToList());
        }

        //
        // GET: /RoomManager/Details/5

        public ViewResult Details(int id)
        {
            Room room = db.Rooms.Find(id);
            return View(room);
        }

        //
        // GET: /RoomManager/Create

        public ActionResult Create()
        {
            ViewBag.RoomTypeID = new SelectList(db.RoomTypes, "RoomTypeID", "TypeName");
            ViewBag.RoomStateID = new SelectList(db.RoomStates, "RoomStateID", "Name");
            return View();
        } 

        //
        // POST: /RoomManager/Create

        [HttpPost]
        public ActionResult Create(Room room)
        {
            if (ModelState.IsValid)
            {
                db.Rooms.Add(room);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }

            ViewBag.RoomTypeID = new SelectList(db.RoomTypes, "RoomTypeID", "TypeName", room.RoomTypeID);
            ViewBag.RoomStateID = new SelectList(db.RoomStates, "RoomStateID", "Name", room.RoomStateID);
            return View(room);
        }
        
        //
        // GET: /RoomManager/Edit/5
 
        public ActionResult Edit(int id)
        {
            Room room = db.Rooms.Find(id);
            ViewBag.RoomTypeID = new SelectList(db.RoomTypes, "RoomTypeID", "TypeName", room.RoomTypeID);
            ViewBag.RoomStateID = new SelectList(db.RoomStates, "RoomStateID", "Name", room.RoomStateID);
            return View(room);
        }

        //
        // POST: /RoomManager/Edit/5

        [HttpPost]
        public ActionResult Edit(Room room)
        {
            if (ModelState.IsValid)
            {
                db.Entry(room).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.RoomTypeID = new SelectList(db.RoomTypes, "RoomTypeID", "TypeName", room.RoomTypeID);
            ViewBag.RoomStateID = new SelectList(db.RoomStates, "RoomStateID", "Name", room.RoomStateID);
            return View(room);
        }

        //
        // GET: /RoomManager/Delete/5
 
        public ActionResult Delete(int id)
        {
            Room room = db.Rooms.Find(id);
            return View(room);
        }

        //
        // POST: /RoomManager/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            Room room = db.Rooms.Find(id);
            db.Rooms.Remove(room);
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        #region RoomGrid
        public ActionResult Select()
        {
            return View("Index", db.Rooms.ToList());
        }
        public ActionResult Insert()
        {
            Room room=new Room();
            if(TryUpdateModel(room))
            {
                db.Rooms.Add(room);
                db.SaveChanges();
                return RedirectToAction("Index"); 
            }
            return View("Index", db.Rooms.ToList());
        }
        public ActionResult Save(int id)
        {
            Room room = new Room {RoomID = id};
            if(TryUpdateModel(room))
            {
                db.Entry(room).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View("Index", db.Rooms.ToList());
        }
        public ActionResult Delete()
        {
            return View("Index", db.Rooms.ToList());
        }
        #endregion

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}